package com.maoatao.easycodegen.framework.model;

import com.maoatao.easycodegen.framework.constant.Order;
import com.maoatao.easycodegen.toolkit.GenericTypeUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 请求基础类
 * 包含所有公共参数，作为各种Request类的父类
 *
 * @author MaoAtao
 * @date 2025-10-04
 */
@Getter
@Setter
public abstract class BaseRequest<K extends Serializable, Q extends BaseQuery<K, Q>> extends PageRequest {

    private final Class<K> idClass;

    private final Class<Q> queryClass;

    /**
     * 主键ID
     */
    private K id;
    /**
     * 主键ID列表
     */
    private List<K> ids;

    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtCreate;
    /**
     * 创建时间起始值
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtCreateStart;
    /**
     * 创建时间结束值
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtCreateEnd;
    /**
     * 修改时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtModified;
    /**
     * 修改时间起始值
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtModifiedStart;
    /**
     * 修改时间结束值
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtModifiedEnd;

    /**
     * 升序排序字段
     */
    private String orderAscBy;

    /**
     * 降序排序字段
     */
    private String orderDescBy;

    @SuppressWarnings("unchecked")
    public BaseRequest() {
        Class<?>[] genericClass = GenericTypeUtil.getGenericClass(getClass());
        if (genericClass == null || genericClass.length < 2) {
            throw new IllegalArgumentException("无法获取泛型参数");
        }
        this.idClass = (Class<K>) genericClass[0];
        this.queryClass = (Class<Q>) genericClass[1];
    }

    /**
     * 转换为Query对象
     *
     * @return Query对象
     */
    public Q toQuery() {
        Q query = BeanUtils.instantiateClass(queryClass);

        // 处理分页参数
        if (getPageNo() != null) {
            query.pageNo(getPageNo());
        }
        if (getPageSize() != null) {
            query.pageSize(getPageSize());
        }

        // 处理ID相关条件
        if (getId() != null) {
            query.idEqualTo(getId());
        }
        if (getIds() != null) {
            query.idIn(getIds());
        }

        // 处理创建时间相关条件
        if (getGmtCreate() != null) {
            query.gmtCreateEqualTo(getGmtCreate());
        }
        if (getGmtCreateStart() != null) {
            query.gmtCreateGreaterThanOrEqualTo(getGmtCreateStart());
        }
        if (getGmtCreateEnd() != null) {
            query.gmtCreateLessThanOrEqualTo(getGmtCreateEnd());
        }

        // 处理修改时间相关条件
        if (getGmtModified() != null) {
            query.gmtModifiedEqualTo(getGmtModified());
        }
        if (getGmtModifiedStart() != null) {
            query.gmtModifiedGreaterThanOrEqualTo(getGmtModifiedStart());
        }
        if (getGmtModifiedEnd() != null) {
            query.gmtModifiedLessThanOrEqualTo(getGmtModifiedEnd());
        }

        // 处理排序
        if (getOrderAscBy() != null) {
            query.orderBy(getOrderAscBy(), Order.ASC);
        }
        if (getOrderDescBy() != null) {
            query.orderBy(getOrderDescBy(), Order.DESC);
        }

        return query;
    }

    @Override
    public void setPageNo(Long pageNo) {
        super.setPageNo(pageNo);
    }

    @Override
    public void setPageSize(Long pageSize) {
        super.setPageSize(pageSize);
    }
}
